// 菜谱数据
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

// 菜谱的数据类型
export interface DishItem {
    breakfast: string[]
    snack: string[] // 小吃
    lunch: {
        staple: string
        main: string[]
        vegetable: string[]
        soup: string
    }
    dinner: {
        staple: string
        dish: string[]
        soup: string
    }
}

// 周菜谱的数据类型
export interface WeeklyMenu {
    week: number
    meals: Record<'monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday', DishItem> // 每天菜谱
}

// 月份菜谱的数据类型
export interface MonthDishData {
    month: number // 1-12
    weeklyMenus: WeeklyMenu[] // 每周菜谱
}


// 每日菜单的数据
const daysMeals = {
    monday: {
        breakfast: ['牛奶', '花卷', '鸡蛋'],
        snack: ['苹果'],
        lunch: {
            staple: '米饭',
            main: ['红烧鸡块'],
            vegetable: ['清炒菠菜'],
            soup: '紫菜蛋花汤'
        },
        dinner: {
            staple: '红薯饭',
            dish: ['西红柿炒蛋', '手撕包菜'],
            soup: '冬瓜排骨汤'
        }
    },
    tuesday: {
        breakfast: ['豆浆', '油条', '小咸菜'],
        snack: ['香蕉'],
        lunch: {
            staple: '米饭',
            main: ['糖醋里脊'],
            vegetable: ['蒜蓉空心菜'],
            soup: '海带汤'
        },
        dinner: {
            staple: '馒头',
            dish: ['宫保鸡丁', '炒青菜'],
            soup: '番茄蛋汤'
        }
    },
    wednesday: {
        breakfast: ['小米粥', '包子', '鸡蛋'],
        snack: ['橙子'],
        lunch: {
            staple: '米饭',
            main: ['鱼香肉丝'],
            vegetable: ['炒油麦菜'],
            soup: '黄瓜蛋汤'
        },
        dinner: {
            staple: '玉米饭',
            dish: ['青椒土豆丝', '蒸南瓜'],
            soup: '紫菜汤'
        }
    },
    thursday: {
        breakfast: ['八宝粥', '煎饼', '咸鸭蛋'],
        snack: ['梨'],
        lunch: {
            staple: '米饭',
            main: ['红烧排骨'],
            vegetable: ['炒西兰花'],
            soup: '豆腐汤'
        },
        dinner: {
            staple: '面条',
            dish: ['肉末茄子', '凉拌黄瓜'],
            soup: '西红柿蛋汤'
        }
    },
    friday: {
        breakfast: ['酸奶', '三明治'],
        snack: ['葡萄'],
        lunch: {
            staple: '米饭',
            main: ['咖喱鸡'],
            vegetable: ['炒豆角'],
            soup: '菌菇汤'
        },
        dinner: {
            staple: '杂粮饭',
            dish: ['木须肉', '炒小白菜'],
            soup: '萝卜汤'
        }
    }
}

// 所有月份菜谱数据
export const allMonthDish: Record<number, MonthDishData> = {
    // 1月：
    1: {
        month: 1,
        weeklyMenus: [
            {
                // 第一周
                week: 1,
                meals: {
                    monday: {
                        breakfast: ['牛奶', '花卷', '鸡蛋'],
                        snack: ['苹果'],
                        lunch: {
                            staple: '米饭',
                            main: ['红烧鸡块'],
                            vegetable: ['清炒菠菜'],
                            soup: '紫菜蛋花汤'
                        },
                        dinner: {
                            staple: '红薯饭',
                            dish: ['西红柿炒蛋', '手撕包菜'],
                            soup: '冬瓜排骨汤'
                        }
                    },
                    tuesday: {
                        breakfast: ['豆浆', '油条', '小咸菜'],
                        snack: ['香蕉'],
                        lunch: {
                            staple: '米饭',
                            main: ['糖醋里脊'],
                            vegetable: ['蒜蓉空心菜'],
                            soup: '海带汤'
                        },
                        dinner: {
                            staple: '馒头',
                            dish: ['宫保鸡丁', '炒青菜'],
                            soup: '番茄蛋汤'
                        }
                    },
                    wednesday: {
                        breakfast: ['小米粥', '包子', '鸡蛋'],
                        snack: ['橙子'],
                        lunch: {
                            staple: '米饭',
                            main: ['鱼香肉丝'],
                            vegetable: ['炒油麦菜'],
                            soup: '黄瓜蛋汤'
                        },
                        dinner: {
                            staple: '玉米饭',
                            dish: ['青椒土豆丝', '蒸南瓜'],
                            soup: '紫菜汤'
                        }
                    },
                    thursday: {
                        breakfast: ['八宝粥', '煎饼', '咸鸭蛋'],
                        snack: ['梨'],
                        lunch: {
                            staple: '米饭',
                            main: ['红烧排骨'],
                            vegetable: ['炒西兰花'],
                            soup: '豆腐汤'
                        },
                        dinner: {
                            staple: '面条',
                            dish: ['肉末茄子', '凉拌黄瓜'],
                            soup: '西红柿蛋汤'
                        }
                    },
                    friday: {
                        breakfast: ['酸奶', '三明治'],
                        snack: ['葡萄'],
                        lunch: {
                            staple: '米饭',
                            main: ['咖喱鸡'],
                            vegetable: ['炒豆角'],
                            soup: '菌菇汤'
                        },
                        dinner: {
                            staple: '杂粮饭',
                            dish: ['木须肉', '炒小白菜'],
                            soup: '萝卜汤'
                        }
                    }
                }
            }
        ]
    },

    // 自动生成2-12月向上面月份一样的数据
    ...Array.from({ length: 11 }, (_, i) => {
        const month = i + 2
        return {
            [month]: {
                month,
                weeklyMenus: Array.from({ length: 4 }, (_, weekIndex) => ({
                    week: weekIndex + 1,
                    meals: { ...daysMeals } // 每周都复制完整数据
                }))
            }
        }
    }).reduce((acc, item) => ({ ...acc, ...item }), {})
}

export const useDishStore = defineStore('dish', () => {
    // 当前选中的月份
    const selectedMonth = ref<number | null>(new Date().getMonth() + 1)

    // 全部菜谱数据
    const monthlyDishes = ref<Record<number, MonthDishData>>(allMonthDish)

    // 获取当前月份的菜谱
    const monthDishList = computed(() => {
        if (!selectedMonth.value) {
            return []
        }

        const month = selectedMonth.value
        const monthData = monthlyDishes.value[month]

        // 如果这个月的数据存在，并且有 weeklyMenus，就返回,否则返回空数组，防止出错
        if (monthData && monthData.weeklyMenus) {
            return monthData.weeklyMenus
        } else {
            return []
        }
    })

    // 设置当前月份
    const setMonth = (month: number | string | Date) => {
        let targetMonth: number

        if (typeof month === 'object' && month instanceof Date) {
            targetMonth = month.getMonth() + 1
        } else {
            targetMonth = Number(month)
        }

        // 只有当月份是 1 到 12 之间时，才更新选中的月份
        if (targetMonth >= 1 && targetMonth <= 12) {
            selectedMonth.value = targetMonth
        }
    }

    // 重置
    const reset = () => {
        selectedMonth.value = null
    }

    return {
        selectedMonth,
        monthlyDishes,
        monthDishList,
        setMonth,
        reset
    }
})